package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.pojo.goods.Spu;
import com.qingcheng.service.goods.CategoryService;
import com.qingcheng.service.goods.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Welcome to the most Amazing IntelliJ IIIDEA.
 * Creat0r: ^_^ fffantianxing ^_^
 * Date: 2019-07-02
 * Time: 14:33
 */
@RestController
@RequestMapping("item")
public class ItemController {

    @Reference
    private SpuService spuService;

    @Reference
    private CategoryService categoryService;

    @Value("${pagePath}")
    private String pagePath;

    @Autowired
    private TemplateEngine templateEngine;

    @RequestMapping("createPage")
    public void createPage(String spuId){
        //查询商品信息
        Goods goods = spuService.findGoodsById(spuId);
        //获取spu信息
        Spu spu = goods.getSpu();
        //获取sku集合
        List<Sku> skuList = goods.getSkuList();
        List<String> categoryList = new ArrayList<>();
        categoryList.add(categoryService.findById(spu.getCategory1Id()).getName());
        categoryList.add(categoryService.findById(spu.getCategory2Id()).getName());
        categoryList.add(categoryService.findById(spu.getCategory3Id()).getName());

        for (Sku sku : skuList) {
            //创建上下文模型
            Context context = new Context();
            Map<String,Object> dataModel = new HashMap<>();
            dataModel.put("sku",sku);
            dataModel.put("spu",spu);
            dataModel.put("categoryList",categoryList);
            dataModel.put("spuImages",spu.getImages().split(","));
            dataModel.put("skuImages",sku.getImages().split(","));
            Map paras = JSON.parseObject(spu.getParaItems());
            dataModel.put("paras",paras);
            Map specs = JSON.parseObject(sku.getSpec());
            dataModel.put("specs",specs);
            Map<String,List> specMap = (Map)JSON.parseObject(spu.getSpecItems());
            //{"颜色":["金色","黑色","蓝色"],"版本":["6GB+64GB"]}spec_items
            //{'颜色': '黑色'}spec
            //{"颜色":[{option:黑色,selected:true},{option:金色,selected:false},{option:灰色,selected:false}],"版本":["6GB+64GB"]}
//            for (String key : specMap.keySet()) {
//                List<String> items = specMap.get(key);//["金色","黑色","蓝色"]
//                List<Map> mapList = new ArrayList<>();//[{option:黑色,selected:true},{option:灰色,selected:false}]
//                for (String item : items) {
//                    Map map = new HashMap();
//                    map.put("option",item);
//                    if (item.equals(specs.get(key))){
//                        map.put("checked",true);//如果规格和当前规格相同，则选中
//                    }else{
//                        map.put("checked",false);
//                    }
//                    mapList.add(map);
//                }
//                specMap.put(key,mapList);
//            }




            dataModel.put("specMap",specMap);
            context.setVariables(dataModel);


            //准备文件
            File dir = new File(pagePath);
            if (!dir.exists()){
                dir.mkdirs();
            }

            File dest = new File(dir,sku.getId()+".html");

            //生成模板
            try {
                PrintWriter printWriter = new PrintWriter(dest,"UTF-8");
                templateEngine.process("item",context,printWriter);
                System.out.println("生成页面"+sku.getId()+".html");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }


    }
}
